//
//  RadarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/5.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "RadarChartViewController.h"
@interface RadarChartViewController ()
@end

@implementation RadarChartViewController
RadarChartView *sampleRadarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleRadarChartView=[[RadarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleRadarChartView.backgroundColor = [UIColor whiteColor];
    sampleRadarChartView.delegate = self;
    [self.view addSubview:sampleRadarChartView];
    [self initChartStyleAndData];
    [sampleRadarChartView animateWithXAxisDuration:1.4 yAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
}

-(void)initChartStyleAndData{
    [self setupRadarChartView:sampleRadarChartView];
    [self setRadarChartViewParam:sampleRadarChartView];
    [self setLegendStyle:sampleRadarChartView];
    [self initChartData];
}

- (void)setupRadarChartView:(RadarChartView *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    chartView.webLineWidth = .75;
    chartView.innerWebLineWidth = 0.375;
    chartView.webAlpha = 1.0;
}

-(void)setRadarChartViewParam:(RadarChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
    
    ChartYAxis *yAxis = chartView.yAxis;
    yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
    yAxis.labelCount = 5;
    yAxis.axisMinValue = 0.0;
}

//设置图例样式
-(void)setLegendStyle:(RadarChartView *)chartView{
    
    ChartLegend *l = chartView.legend;
    l.position = ChartLegendPositionRightOfChart;
    l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    l.xEntrySpace = 7.0;
    l.yEntrySpace = 5.0;
}

- (void)initChartData
{
    [self setDataCount:9 chart:sampleRadarChartView];
}

- (void)setDataCount:(int)count chart:(RadarChartView *)chartView
{
    double mult = 150.f;
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [yVals1 addObject:[[ChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 2) xIndex:i]];
        [yVals2 addObject:[[ChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 2) xIndex:i]];
    }
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    RadarChartDataSet *set1 = [[RadarChartDataSet alloc] initWithYVals:yVals1 label:@"Set 1"];
    [set1 setColor:ChartColorTemplates.vordiplom[0]];
    set1.fillColor = ChartColorTemplates.vordiplom[0];
    set1.drawFilledEnabled = YES;
    set1.lineWidth = 2.0;
    
    RadarChartDataSet *set2 = [[RadarChartDataSet alloc] initWithYVals:yVals2 label:@"Set 2"];
    [set2 setColor:ChartColorTemplates.vordiplom[4]];
    set2.fillColor = ChartColorTemplates.vordiplom[4];
    set2.drawFilledEnabled = YES;
    set2.lineWidth = 2.0;
    
    RadarChartData *data = [[RadarChartData alloc] initWithXVals:xVals dataSets:@[set1, set2]];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:8.f]];
    [data setDrawValues:NO];
    
    chartView.data = data;
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end